package com.zhenhao.xsq.ui.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseActivity;
import com.zhenhao.xsq.entity.EvenbugMessage;
import com.zhenhao.xsq.entity.TabDataBean;
import com.zhenhao.xsq.ui.activity.home.component.DaggerHomeComponent;
import com.zhenhao.xsq.ui.activity.home.contract.HomeContract;
import com.zhenhao.xsq.ui.activity.home.module.HomeModule;
import com.zhenhao.xsq.ui.activity.home.presenter.HomePresenter;
import com.zhenhao.xsq.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import androidx.fragment.app.FragmentTabHost;
import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity implements HomeContract.View {

    @Inject
    HomePresenter mPresenter;

    @BindView(R.id.contentLayout)
    FrameLayout contentLayout;
    @BindView(android.R.id.tabcontent)
    FrameLayout tabcontent;
    @BindView(android.R.id.tabhost)
    FragmentTabHost tabhost;

    private ArrayList<TabDataBean> tabDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tabhost.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        autolayout.setVisibility(View.GONE);
        initTabHost();

        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        loadNewVersion();
    }

    private void initTabHost() {
        //初始化fTabHost, 第三个参数为内容容器
        tabhost.setup(this, getSupportFragmentManager(), R.id.contentLayout);
        /*初始化数据源*/
        TabDataBean tabHome = new TabDataBean(getResources().getString(R.string.智慧服务), R.drawable.select_tab_home_index, IndexFragment
                .class);
        TabDataBean tabHot = new TabDataBean(getResources().getString(R.string.我的), R.drawable.select_tab_home_mine,
                MineFragment.class);

        tabDataList.clear();
        tabDataList.add(tabHome);
//        tabDataList.add(tabmall);
//        tabDataList.add(tabCategory);
        tabDataList.add(tabHot);
        //添加底部菜单项-tabSpec
        for (TabDataBean bean : tabDataList) {
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(bean.getTabName());
            //给菜单项添加内容，indicator,其中indicator需要的参数View即为菜单项的布局
            tabSpec.setIndicator(getIndicatorView(bean));
            //第二参数就是该菜单项对应的页面内容
            tabhost.addTab(tabSpec, bean.getContent(), null);
        }
        tabhost.setCurrentTab(0);
    }

    private View getIndicatorView(TabDataBean bean) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_home_title, null);
        ImageView iconTab = view.findViewById(R.id.iv_tab_icon);
        TextView tvTab = view.findViewById(R.id.tv_tab_label);
        iconTab.setImageResource(bean.getTabIcon());
        tvTab.setText(bean.getTabName());
        return view;
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(AppConfig.getInstance(), getResources().getString(R.string.再按一次退出APP), Toast.LENGTH_LONG);
                mExitTime = System.currentTimeMillis();
            } else {
                ActivityUtils.finishAllActivities();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadNewVersion() {
//        Map<String, String> map = new HashMap<>();
//        map.put("versionGongsiName", Contains.APP_TYPE + "");//1x信宜管家2欣管家3欣助手(自用)5欣社区
//        map.put("type", "2");//1欣助手，2欣社区
//        mPresenter.getVersion(map);
    }

    @Override
    protected void setupActivityComponent() {
        DaggerHomeComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setPresenter(HomeContract.HomeContractPresenter presenter) {
        mPresenter = (HomePresenter) presenter;
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (mDownloadManager != null) {
//            mDownloadManager.resume();
//        }
//        //默认toolbar主题 白色
//        SpSaveUtils.putToolbarColor(false);
    }

    @Override
    public void onPause() {
        super.onPause();
//        if (mDownloadManager != null) {
//            mDownloadManager.onPause();
//        }
    }

    @Override
    protected void onDestroy() {
//        unbindService();
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        mPresenter.unsubscribe();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(EvenbugMessage message) {
        if (message.getType() == EvenbugMessage.MSG_HOME_REFRESH) {
            Log.e("刷新", "HomeActivity 响应");
//            reLoginRtc();
        }
    }
}
